package com.bigData.main.TestHDFS;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import java.io.IOException;

@org.springframework.context.annotation.Configuration // 正确应用Spring的配置注解
public class HdfsConfig {

    @Value("${hadoop.namenode:node1}")
    private String namenode;

    @Bean(name = "hadoopConfiguration")
    public Configuration hadoopConfig() {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://" + namenode + ":9000");
        conf.set("dfs.client.use.datanode.hostname", "true");
        conf.set("dfs.replication", "1");
        return conf;
    }

    // 使用类型注入
    @Bean
    public FileSystem hdfsFileSystem(@Autowired Configuration hadoopConfig) throws IOException {
        return FileSystem.get(hadoopConfig);
    }
}